package com.whynot.frame.manythread;

/**
 * 测试锁的有效范围
 * 在方法上，就只针对本方法
 * @author Administrator
 *
 */
public class MyObject {
	private synchronized void test1(){
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("1");
	}
	private void test2(){
		System.out.println("2");
	}
	public static void main(String[] args) {
		final MyObject obj = new MyObject();
		Thread t1 = new Thread( new Runnable() {
			public void run() {
				obj.test1();
			}
		});
		Thread t2 = new Thread( new Runnable() {
			public void run() {
				obj.test2();
			}
		});
		Thread t3 = new Thread( new Runnable() {
			public void run() {
				obj.test1();
			}
		});
		Thread t4 = new Thread( new Runnable() {
			public void run() {
				obj.test2();
			}
		});
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
